<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>手机商城</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: #f8fafc;
            color: #2c3e50;
            line-height: 1.6;
        }

        .main-container {
            display: flex;
            min-height: 100vh;
        }

        /* 左侧导航栏 */
        .sidebar {
            width: 250px;
            background: linear-gradient(135deg, #2c3e50, #34495e);
            color: white;
            position: fixed;
            height: 100vh;
            overflow-y: auto;
            z-index: 1000;
        }

        .sidebar-header {
            padding: 2rem 1.5rem;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }

        .logo {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            text-decoration: none;
            color: white;
        }

        .logo-icon {
            font-size: 2rem;
        }

        .site-title {
            font-size: 1.5rem;
            font-weight: 700;
            margin-bottom: 0.5rem;
        }

        .site-subtitle {
            opacity: 0.8;
            font-size: 0.9rem;
        }

        .sidebar-nav {
            padding: 1rem 0;
        }

        .nav-item {
            display: block;
            padding: 1rem 1.5rem;
            color: rgba(255,255,255,0.9);
            text-decoration: none;
            transition: all 0.3s;
            border-left: 3px solid transparent;
        }

        .nav-item:hover {
            background: rgba(255,255,255,0.1);
            color: white;
        }

        .nav-item.active {
            background: rgba(52, 152, 219, 0.2);
            border-left-color: #3498db;
            color: white;
        }

        .nav-icon {
            margin-right: 0.75rem;
            width: 20px;
            display: inline-block;
        }

        .user-section {
            padding: 1rem 1.5rem;
            border-top: 1px solid rgba(255,255,255,0.1);
            margin-top: auto;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 0.75rem;
            margin-bottom: 1rem;
        }

        .user-avatar {
            width: 40px;
            height: 40px;
            background: #3498db;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.2rem;
        }

        .user-details {
            flex: 1;
        }

        .user-name {
            font-weight: 600;
            font-size: 0.9rem;
        }

        .user-role {
            font-size: 0.8rem;
            opacity: 0.7;
        }

        .auth-btn {
            width: 100%;
            background: #3498db;
            color: white;
            border: none;
            padding: 0.75rem;
            border-radius: 8px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            text-decoration: none;
            text-align: center;
            display: block;
        }

        .auth-btn:hover {
            background: #2980b9;
        }

        /* 主内容区域 */
        .main-content {
            flex: 1;
            margin-left: 250px;
            padding: 2rem;
        }

        .content-header {
            background: white;
            padding: 1.5rem 2rem;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.08);
            margin-bottom: 2rem;
        }

        .page-title {
            font-size: 2rem;
            font-weight: 700;
            color: #2c3e50;
            margin-bottom: 0.5rem;
        }

        .page-description {
            color: #7f8c8d;
            font-size: 1.1rem;
        }



        /* 商品展示区域 */
        .products-section {
            background: white;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.08);
            overflow: hidden;
        }

        .section-header {
            background: #f8f9fa;
            padding: 1.5rem 2rem;
            border-bottom: 1px solid #e9ecef;
        }

        .section-title {
            font-size: 1.2rem;
            font-weight: 600;
            color: #2c3e50;
        }

        .products-container {
            padding: 2rem;
        }

        /* 商品网格 */
        .products-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 1.5rem;
        }

        .product-card {
            background: white;
            border: 1px solid #e9ecef;
            border-radius: 12px;
            overflow: hidden;
            transition: all 0.3s;
            position: relative;
        }

        .product-card:hover {
            box-shadow: 0 8px 30px rgba(0,0,0,0.12);
            transform: translateY(-2px);
            border-color: #3498db;
        }

        .product-image-container {
            height: 200px;
            background: #f8f9fa;
            display: flex;
            align-items: center;
            justify-content: center;
            position: relative;
            border-bottom: 1px solid #e9ecef;
        }

        .product-icon {
            font-size: 4rem;
            color: #3498db;
        }

        .product-badge {
            position: absolute;
            top: 1rem;
            right: 1rem;
            background: #3498db;
            color: white;
            padding: 0.25rem 0.75rem;
            border-radius: 15px;
            font-size: 0.8rem;
            font-weight: 600;
        }

        .product-content {
            padding: 1.5rem;
        }

        .product-brand {
            color: #3498db;
            font-size: 0.85rem;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin-bottom: 0.5rem;
        }

        .product-name {
            font-size: 1.2rem;
            font-weight: 600;
            color: #2c3e50;
            margin-bottom: 0.75rem;
            line-height: 1.3;
        }

        .product-description {
            color: #7f8c8d;
            font-size: 0.9rem;
            margin-bottom: 1rem;
            line-height: 1.5;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        .product-price {
            font-size: 1.5rem;
            font-weight: 700;
            color: #e74c3c;
            margin-bottom: 1rem;
        }

        .product-meta {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 1rem;
            padding: 0.75rem;
            background: #f8f9fa;
            border-radius: 8px;
            font-size: 0.85rem;
            color: #7f8c8d;
        }

        .stock-status {
            color: #27ae60;
            font-weight: 500;
        }

        .product-actions {
            display: flex;
            gap: 0.75rem;
        }

        .btn {
            flex: 1;
            padding: 0.75rem 1rem;
            border: none;
            border-radius: 8px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 0.9rem;
        }

        .btn-primary {
            background: #3498db;
            color: white;
        }

        .btn-primary:hover {
            background: #2980b9;
            transform: translateY(-1px);
        }

        .btn-secondary {
            background: #f8f9fa;
            color: #2c3e50;
            border: 2px solid #e9ecef;
        }

        .btn-secondary:hover {
            background: #e9ecef;
            border-color: #3498db;
            color: #3498db;
        }

        /* 加载和空状态 */
        .loading, .empty-state {
            text-align: center;
            padding: 4rem 2rem;
            color: #7f8c8d;
        }

        .loading-spinner {
            font-size: 3rem;
            animation: spin 1s linear infinite;
            margin-bottom: 1rem;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .empty-icon {
            font-size: 4rem;
            margin-bottom: 1rem;
            opacity: 0.5;
        }

        /* 消息提示 */
        .message {
            position: fixed;
            top: 2rem;
            right: 2rem;
            padding: 1rem 1.5rem;
            border-radius: 8px;
            font-weight: 500;
            z-index: 1000;
            transform: translateX(400px);
            transition: transform 0.3s;
            max-width: 350px;
        }

        .message.show {
            transform: translateX(0);
        }

        .message.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .message.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .sidebar {
                transform: translateX(-100%);
                transition: transform 0.3s;
            }

            .sidebar.open {
                transform: translateX(0);
            }

            .main-content {
                margin-left: 0;
                padding: 1rem;
            }

            .products-grid {
                grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
                gap: 1rem;
            }



            .message {
                right: 1rem;
                left: 1rem;
                transform: translateY(-100px);
                max-width: none;
            }

            .message.show {
                transform: translateY(0);
            }
        }

        /* 移动端菜单按钮 */
        .mobile-menu-btn {
            display: none;
            position: fixed;
            top: 1rem;
            left: 1rem;
            z-index: 1001;
            background: #3498db;
            color: white;
            border: none;
            padding: 0.75rem;
            border-radius: 8px;
            cursor: pointer;
            font-size: 1.2rem;
        }

        @media (max-width: 768px) {
            .mobile-menu-btn {
                display: block;
            }
        }
    </style>
</head>

<body>
    <div class="main-container">
        <!-- 左侧导航栏 -->
        <aside class="sidebar" id="sidebar">
            <div class="sidebar-header">
                <a href="index.html" class="logo">
                    <span class="logo-icon"></span>
                </a>
                <div class="site-title">智能手机商城</div>
                <div class="site-subtitle">精选优质手机产品</div>
            </div>

            <nav class="sidebar-nav">
                <a href="index.html" class="nav-item active">
                    <span class="nav-icon"></span>
                    首页商城
                </a>
                <a href="cart.html" class="nav-item">
                    <span class="nav-icon"></span>
                    购物车
                </a>
                <a href="orders.html" class="nav-item">
                    <span class="nav-icon"></span>
                    我的订单
                </a>
            </nav>

            <div class="user-section">
                <div class="user-info" id="userInfoSection" style="display: none;">
                    <div class="user-avatar"></div>
                    <div class="user-details">
                        <div class="user-name" id="userName">用户</div>
                        <div class="user-role">普通用户</div>
                    </div>
                </div>
                <a href="login.html" class="auth-btn" id="authBtn">登录</a>
            </div>
        </aside>

        <!-- 主内容区域 -->
        <main class="main-content">
            <!-- 页面标题 -->
            <div class="content-header">
                <h1 class="page-title">商品展示</h1>
                <p class="page-description">发现最新、最热门的智能手机，找到最适合您的那一款</p>
            </div>



            <!-- 商品展示区域 -->
            <div class="products-section">
                <div class="section-header">
                    <h2 class="section-title">精选手机产品</h2>
                </div>

                <div class="products-container">
                    <!-- 加载状态 -->
                    <div id="loadingIndicator" class="loading" style="display: none;">
                        <div class="loading-spinner"></div>
                        <div>正在加载商品...</div>
                    </div>

                    <!-- 商品网格 -->
                    <div id="productsGrid" class="products-grid">
                        <!-- 商品将在这里动态加载 -->
                    </div>
                </div>
            </div>
        </main>
    </div>

    <!-- 移动端菜单按钮 -->
    <button class="mobile-menu-btn" id="mobileMenuBtn">☰</button>

    <!-- 消息提示 -->
    <div id="message" class="message"></div>

    <script>
        let currentUser = null;
        let products = [];

        // 初始化
        document.addEventListener('DOMContentLoaded', function() {
            initializeAuth();
            loadProducts();
            initializeMobileMenu();
        });

        // 初始化认证状态
        function initializeAuth() {
            const userData = localStorage.getItem('currentUser');
            if (userData) {
                currentUser = JSON.parse(userData);
                document.getElementById('userInfoSection').style.display = 'flex';
                document.getElementById('userName').textContent = currentUser.username;
                document.getElementById('authBtn').textContent = '退出登录';
                document.getElementById('authBtn').href = '#';
                document.getElementById('authBtn').onclick = logout;
            }
        }

        // 初始化移动端菜单
        function initializeMobileMenu() {
            const mobileMenuBtn = document.getElementById('mobileMenuBtn');
            const sidebar = document.getElementById('sidebar');

            mobileMenuBtn.addEventListener('click', function() {
                sidebar.classList.toggle('open');
            });

            // 点击主内容区域关闭菜单
            document.querySelector('.main-content').addEventListener('click', function() {
                sidebar.classList.remove('open');
            });
        }

        // 加载商品
        async function loadProducts() {
            showLoading(true);

            try {
                const response = await fetch('/products');
                if (response.ok) {
                    products = await response.json();
                    renderProducts();
                } else {
                    showEmptyState('加载商品失败，请刷新页面重试');
                }
            } catch (error) {
                console.error('加载商品错误:', error);
                showEmptyState('网络连接失败，请检查网络后重试');
            } finally {
                showLoading(false);
            }
        }



        // 渲染商品
        function renderProducts() {
            const container = document.getElementById('productsGrid');
            
            if (products.length === 0) {
                showEmptyState('暂时没有商品，请稍后再来看看');
                return;
            }

            // 只显示上架的商品
            const availableProducts = products.filter(product => product.productStatus === 1);
            
            if (availableProducts.length === 0) {
                showEmptyState('暂时没有可购买的商品');
                return;
            }

            let html = '';
            availableProducts.forEach(product => {
                html += `
                    <div class="product-card">
                        <div class="product-image-container">
                            <div class="product-icon">📱</div>
                            <div class="product-badge">热销</div>
                        </div>
                        <div class="product-content">
                            <div class="product-brand">${product.productBrand || '精品手机'}</div>
                            <h3 class="product-name">${product.productName}</h3>
                            <p class="product-description">${product.productDescription || '高品质智能手机，为您带来卓越的使用体验。采用最新技术，性能卓越，设计精美。'}</p>
                            
                            <div class="product-price">¥${product.productPrice.toFixed(2)}</div>
                            
                            <div class="product-meta">
                                <span>库存：${product.stockQuantity}台</span>
                                <span class="stock-status">现货</span>
                            </div>
                            
                            <div class="product-actions">
                                <button class="btn btn-primary" onclick="addToCart(${product.productId})">
                                    加入购物车
                                </button>
                                <button class="btn btn-secondary" onclick="viewDetails(${product.productId})">
                                    查看详情
                                </button>
                            </div>
                        </div>
                    </div>
                `;
            });

            container.innerHTML = html;
        }

        // 显示加载状态
        function showLoading(show) {
            document.getElementById('loadingIndicator').style.display = show ? 'block' : 'none';
            document.getElementById('productsGrid').style.display = show ? 'none' : 'grid';
        }

        // 显示空状态
        function showEmptyState(message) {
            const container = document.getElementById('productsGrid');
            container.innerHTML = `
                <div class="empty-state">
                    <div class="empty-icon">📱</div>
                    <div class="empty-message">${message}</div>
                </div>
            `;
            container.style.display = 'block';
        }

        // 添加到购物车
        async function addToCart(productId) {
            if (!currentUser) {
                showMessage('请先登录后再添加商品到购物车', 'error');
                setTimeout(() => {
                    window.location.href = 'login.html';
                }, 2000);
                return;
            }

            try {
                const response = await fetch('/carts', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        userId: currentUser.userId,
                        productId: productId,
                        quantity: 1
                    })
                });

                if (response.ok) {
                    const product = products.find(p => p.productId === productId);
                    showMessage(`${product.productName} 已成功添加到购物车！`, 'success');
                } else {
                    showMessage('添加到购物车失败，请重试', 'error');
                }
            } catch (error) {
                console.error('添加到购物车错误:', error);
                showMessage('网络错误，请稍后重试', 'error');
            }
        }

        // 查看商品详情
        function viewDetails(productId) {
            const product = products.find(p => p.productId === productId);
            if (product) {
                const details = `商品详情：

名称：${product.productName}
品牌：${product.productBrand || '未知'}
价格：¥${product.productPrice}
库存：${product.stockQuantity}台
描述：${product.productDescription || '暂无详细描述'}`;
                alert(details);
            }
        }

        // 登出
        function logout() {
            if (confirm('确定要退出登录吗？')) {
                localStorage.removeItem('currentUser');
                window.location.reload();
            }
        }

        // 显示消息
        function showMessage(text, type = 'success') {
            const messageEl = document.getElementById('message');
            messageEl.textContent = text;
            messageEl.className = `message ${type} show`;

            setTimeout(() => {
                messageEl.classList.remove('show');
            }, 5000);
        }
    </script>
</body>

</html>